package com.cskaoyan.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/5/25
 */
public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory=null;
    static {
        InputStream inputStream=null;
        try {
            inputStream=Resources.getResourceAsStream("mybatis.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    }

    //提供方法获取sqlsession
    public static SqlSession getSqlSession(){
        //oprnSession()->不会自动提交事务，需要手动commit（）
        return sqlSessionFactory.openSession();
    }

    public static SqlSession getSqlSession(boolean autoCommit){
        //oprnSession(autoCommit)->True为自动提交；按需选择
        return sqlSessionFactory.openSession(autoCommit);
    }



}
